<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.css" rel="stylesheet">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.js"></script>

</head>

<body>
    <h1>拦截器练习</h1>
    <input type="text" id="ipt">
    <button id="btn">提交</button>

    <script>
        const myAxios = axios.create({
            baseURL:'/',
            timeout:10000,
            headers:{}
        })

        myAxios.interceptors.request.use(function(config){
            const token = 'token';
            config.headers.token = token;
            NProgress.start();
            return config;
        
        },function(error){
            return Promise.reject(error);
        });

        myAxios.interceptors.raponse.use(function(response){
            NProgress.done();
            if (reponse.data.code !== 10000) {
                return Promise.reject(response.data.msg)
            }
            return Promise.data,data
        },function(error){
            return Promise.reject(error);
        })

        const oIpt = document.getElementById('ipt');
        const oBtn = document.getElementById('btn');
        oBtn.onclick = async function(){
            const iptValue = oIpt.value;
            try{
                const result = await myAxios.get ("/user",{
                    params:{
                        userId:iptValue
                    }
                })
                console.log(result);
            } catch (e){
                console.log(e,'出现错误');
            }
        }
    </script>
</body>

</html>